package com.wyl.idempotentcheck.controller;

import com.wyl.idempotentcheck.annotation.Idempotent;
import com.wyl.idempotentcheck.common.CommonException;
import com.wyl.idempotentcheck.common.R;
import com.wyl.idempotentcheck.entity.User;
import com.wyl.idempotentcheck.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private IUserService userService;

    private Logger logger = LoggerFactory.getLogger(TestController.class);


    /**
     * 自定义注解+Redis+MD5加密的拦截器实现幂等性校验
     * @return
     */
    @Idempotent
    @RequestMapping(value = "/test/idempotent",method = {RequestMethod.GET, RequestMethod.POST})
    public R<User> idempotentTest(@RequestBody User user){
        logger.info(user.toString());
        userService.updateUserById(user);
        return R.success(user);
    }

    /**
     * 异常的处理方法
     * @param ex
     * @return
     */
    @ExceptionHandler(CommonException.class)
    public R exceptionHandler(CommonException ex){
        return R.error(ex.getMsg());
    }

}
